function login() {

    $.ajax({
        type: 'get',
        url: 'http://192.168.10.169/api/user/login/' + $("#username").val() + '/' + $("#password").val(),
        data: $('#login-form').serialize(),
        dataType: 'json',
        async: true,
        success: function (data) {

            alert(data.responseText);

        },
        error: function (data) {

            // alert(data.responseText);


            if (data.responseText.indexOf('欢迎登录登录系统') === -1) {
                alert(data.responseText);
            } else {
                window.location.href = 'welcome.html?msg=' + data.responseText;
            }

        }
    });

}


function register() {


    $.ajax({
        type: 'get',
        url: 'http://192.168.10.169/api/user/register/' + $("#username").val() + '/' + $("#password").val() + '/' + $("#code").val(),
        data: $('#login-form').serialize(),
        dataType: 'json',
        async: true,
        xhrFields: {

            withCredentials: true

        },
        success: function (data) {

            alert(data.responseText);


        },
        error: function (data) {
            // 出错也需要重载，避免重复数据

            if (data.responseText.indexOf('注册成功') === -1) {

                alert(data.responseText);


            } else {
                alert(data.responseText + '点击确定去登录页面登录');
                window.location.href = 'login.html';
            }


        }
    });

}


function getCaptcha() {


    $.ajax({
        type: 'get',
        url: 'http://192.168.10.169/api/code/create/' + $("#username").val(),
        data: $('#login-form').serialize(),
        dataType: 'json',
        async: true,
        success: function (data) {
            alert(data.responseText);

        },
        error: function (data) {
            alert(data.responseText);
        }
    });

}


function getResource() {
    console.log(document.cookie);


    $.ajax({
        type: 'get',
        url: 'http://192.168.10.169/api/user/getResource',
        data: $('#login-form').serialize(),
        dataType: 'json',
        async: true,
        // headers: {'Cookie' : document.cookie },
        xhrFields: {

            withCredentials: true

        },
        success: function (data) {
            alert(data.responseText);

        },
        error: function (data) {
            alert(data.responseText);
        }
    });

}